package com.hup.joystick.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.hup.joystick.constant.FunctionConstant.QuickMenuFunctionType;
import com.hup.joystick.service.QuickMenuFunctions;
import com.hup.utils.commons.TextUtil;
import com.hup.utils.commons.interfaces.Runners.RunAndThrowable;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.awt.event.KeyEvent;
import java.util.List;

/**
 * 快捷菜单功能
 *
 * @author hugan
 * @date 2020/11/10
 */
@Setter
@Getter
@NoArgsConstructor
public class QuickMenuFunction {

    /**
     * 功能类型
     */
    public QuickMenuFunctionType type = QuickMenuFunctionType.CMD;

    /**
     * {@link QuickMenuFunctionType#KEYBOARD}时的键盘按键
     */
    public List<Integer> lstKeyCode;

    /**
     * {@link QuickMenuFunctionType#CMD}时的命令描述
     */
    public String cmdDesc;

    /**
     * 操作
     */
    @JSONField(serialize = false)
    public RunAndThrowable action;

    /**
     * 功能描述,用于界面展示
     */
    @JSONField(serialize = false)
    private String desc;

    public String getDesc() {
        if (desc == null) {
            if (type == QuickMenuFunctionType.KEYBOARD) {
                desc = QuickMenuFunctions.getKeyBoardDesc(TextUtil.getText(lstKeyCode, "+", null, null,
                        (sb, hasNext, next) -> sb.append(KeyEvent.getKeyText(next))));
            } else {
                desc = cmdDesc;
            }
        }
        return desc;
    }

    @Override
    public String toString() {
        if (type == QuickMenuFunctionType.KEYBOARD) {
            return TextUtil.getText(lstKeyCode, "+", "QuickMenuFunction{", "}",
                    (sb, hasNext, next) -> sb.append(KeyEvent.getKeyText(next)));
        } else {
            return "QuickMenuFunction{"
                    + cmdDesc
                    + "}";
        }
    }

}
